from multiprocessing import Process,Pool,Queue,Pipe,Manager

import time
import os

def get_html(times):
    time.sleep(times)
    print(f"sub_process {os.getpid()} {times}")
    return times

def test_process():
    process = Process(target=get_html,args=(2,))
    process.start()
    print(process.pid)
    process.join()

def test_pool():
    pool = Pool(os.cpu_count())
    result1 = pool.apply_async(get_html,(4,))
    result2 = pool.apply_async(get_html,(2,))
    #注意join之前要 close 禁止 pool继续加入任务
    pool.close()
    pool.join()

    print("OVER")

def add_dict(d,k,v):
    d[k] = v

def test_dict():
    d = Manager().dict()
    pool = Pool(os.cpu_count())
    pool.apply_async(add_dict,(d,"tiger",33))
    pool.apply_async(add_dict,(d,"rose",23))
    pool.close()
    pool.join()
    print(d)
    #{'tiger': 33, 'rose': 23}
if __name__ == '__main__':
    test_dict()

